package com.keste.custom.ui.bean;

import com.keste.adf.ui.fwk.util.ADFUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.share.ADFContext;
import oracle.adf.view.rich.component.fragment.UIXDeclarativeComponent;

import oracle.adf.view.rich.component.rich.nav.RichCommandButton;

import oracle.jbo.Row;
import oracle.jbo.domain.Number;


public class QuestionComponentBean {
    
    private UIXDeclarativeComponent qComponent;
    private HashMap questionMap;
    private HashMap qAnswerOptions;
    private HashMap qValidOptions;
    private String questionType;
    private String displayType;
    private Number examId;
    private Number questionId;
    private String questionText;
    
    //private List selectedAnswers;
    private ArrayList<String> selectedAnswers;// = new ArrayList<String>();
    private HashMap qSelectedAnswers = new HashMap();
    private RichCommandButton submitButton;


    public QuestionComponentBean() {
        qComponent = (UIXDeclarativeComponent)getValueObject("#{component}",UIXDeclarativeComponent .class);
//        questionMap = (HashMap)qComponent.getAttributes().get("Question");
//        
//        if(questionMap == null) return;
//        
//        questionId = (Number)questionMap.keySet().toArray()[0];
//        questionText = (String)questionMap.get(questionId);
//        HashMap options = (HashMap)qComponent.getAttributes().get("Options");
//        Object[] keys = options.keySet().toArray();
//        qAnswerOptions = new HashMap();
//        qValidOptions = new HashMap();
//        
//        for(int iIndex = 0; iIndex < keys.length;iIndex++)
//        {
//            Number ky = (Number)keys[iIndex];
//            System.out.println(ky);
//            System.out.println(options.get(ky));
//            Row rAnswer = (Row)options.get(ky);
//            qAnswerOptions.put(ky, rAnswer.getAttribute("AnswerText"));
//            qValidOptions.put(ky, rAnswer.getAttribute("ValidAnswer"));
//        }
        
       // questionType = (String)qComponent.getAttributes().get("QuestionType");
   //     displayType = (String)qComponent.getAttributes().get("DisplayType");
        //
        
    }
    
    private Object getValueObject(String expr, Class returnType){
         FacesContext fc = FacesContext.getCurrentInstance();
        ELContext elctx = fc.getELContext();
        ExpressionFactory elFactory =
        fc.getApplication().getExpressionFactory();
        ValueExpression valueExpr =
        elFactory.createValueExpression(elctx,expr,returnType);
        return valueExpr.getValue(elctx);

    }

    public void setQAnswerOptions(HashMap qAnswerOptions) {
        this.qAnswerOptions = qAnswerOptions;
    }

    public Map getQAnswerOptions() {
        
        return qAnswerOptions;
    }
    
    public Object[] getQuestionOptions() {  
        return this.getQAnswerOptions().keySet().toArray();  
    }

    public void setQComponent(UIXDeclarativeComponent qComponent) {
        this.qComponent = qComponent;
    }

    public UIXDeclarativeComponent getQComponent() {
        return qComponent;
    }

    public void onSelectAnswer(ValueChangeEvent valueChangeEvent) {

        Boolean selected = (Boolean)valueChangeEvent.getNewValue();

        if(qSelectedAnswers.get(questionId) != null)
            selectedAnswers = (ArrayList<String>)qSelectedAnswers.get(questionId);
        else
            selectedAnswers =  new ArrayList<String>();

        String answerId = ((Number)valueChangeEvent.getComponent().getAttributes().get("selectedAnswerId")).toString();

        if(questionType.equals("Q_M_Choice"))
        {
            if(selectedAnswers.contains(answerId) && !selected)
            {
                selectedAnswers.remove(answerId);
            }
            else if(!selectedAnswers.contains(answerId) && selected)
            {
                selectedAnswers.add(answerId);
            }
        }
        else if(questionType.equals("Q_S_Choice") || questionType.equals("Q_B_Choice"))
        {
            if(selected)
            {
                selectedAnswers.clear();
                selectedAnswers.add(answerId);
            }
        }
        
        if(qSelectedAnswers.get(questionId) != null)
            qSelectedAnswers.remove(questionId);
        
        qSelectedAnswers.put(questionId, selectedAnswers);
        
        ADFUtils.addPartialTarget(this.getSubmitButton());
        ADFContext.getCurrent().getViewScope().put("SelectedAnswers", selectedAnswers);
    }
    
    public boolean getIsSingleChoiceRenderable()
    {
        if(getQuestionType() == null || getDisplayType() == null)
            initializeQuestionComponent();
        
        if((this.getQuestionType().equalsIgnoreCase("Q_S_Choice") || this.getQuestionType().equalsIgnoreCase("Q_B_Choice")) && !this.getDisplayType().equalsIgnoreCase("R"))
            return true;
        
        return false;
    }
    
    public boolean getIsMultiChoiceRenderable()
    {
        if(this.getQuestionType().equalsIgnoreCase("Q_M_Choice") && !this.getDisplayType().equalsIgnoreCase("R"))
            return true;
        
        return false;
    }
    
    public boolean getIsQuestionReadOnly()
    {
        if(this.getDisplayType().equalsIgnoreCase("R"))
            return true;
       
        return false;
    }

    public boolean getIsQAvailable()
    {
        if(questionMap != null)
            return true;
        
        return false;
    }

    public void setQuestionText(String questionText) {
        this.questionText = questionText;
    }

    public String getQuestionText() {
        
        initializeQuestionComponent();
              
        return questionText;
    }

    public void setQValidOptions(HashMap qValidOptions) {
        this.qValidOptions = qValidOptions;
    }

    public HashMap getQValidOptions() {
        return qValidOptions;
    }

    public String getQuestionType() {
        return questionType;
    }

    public String getDisplayType() {
        return displayType;
    }

    private void initializeQuestionComponent() {
        
        questionMap = (HashMap)qComponent.getAttributes().get("Question");
        
        questionId = (Number)questionMap.keySet().toArray()[0];
        questionText = (String)questionMap.get(questionId);
        
        /////
        HashMap options = (HashMap)qComponent.getAttributes().get("Options");
        Object[] keys = options.keySet().toArray();
        qAnswerOptions = new HashMap();
        qValidOptions = new HashMap();
        
        for(int iIndex = 0; iIndex < keys.length;iIndex++)
        {
            Number ky = (Number)keys[iIndex];
            System.out.println(ky);
            System.out.println(options.get(ky));
            Row rAnswer = (Row)options.get(ky);
            qAnswerOptions.put(ky, rAnswer.getAttribute("AnswerText"));
            qValidOptions.put(ky, rAnswer.getAttribute("ValidAnswer"));
        }
        ///////
        displayType = (String)qComponent.getAttributes().get("DisplayType");
        questionType = (String)qComponent.getAttributes().get("QuestionType");
        
        examId = (Number)qComponent.getAttributes().get("ExamId");
        
    }
    
    public boolean getIsSubmittable()
    {
        
 //       qComponent.get

//"#{component.handleDoSubmit}"
        
     //  Boolean b = (Boolean)ADFUtils.invokeEL("#{component.handleIsSubmittable}");
       Boolean b = (Boolean)ADFUtils.invokeEL("#{component.handleIsSubmittable}", new Class[]{Number.class,Number.class},new Object[]{examId,questionId} );
      //  ADFUtils.invokeEL(arg0, arg1, arg2);

        if(qSelectedAnswers.get(questionId) != null)
        {
            ArrayList<String> selectAnswers = (ArrayList<String>)qSelectedAnswers.get(questionId);
            if(selectAnswers.size() > 0)
            {
                return true;
            }
        }
        
        return false;
    }

    public void setSubmitButton(RichCommandButton submitButton) {
        this.submitButton = submitButton;
    }

    public RichCommandButton getSubmitButton() {
        return submitButton;
    }
}
